package com.jy.wms.controller;

import com.github.pagehelper.Page;
import com.joyi.gate.comm.SessionInfo;
import com.joyi.gate.safety.vo.UserInfo;
import com.jy.wms.common.SetStatus;
import com.jy.wms.common.base.BaseController;
import com.jy.wms.common.base.ReturnType;
import com.jy.wms.common.base.ServiceException;
import com.jy.wms.common.base.SucessException;
import com.jy.wms.pojo.WmsReview;
import com.jy.wms.service.WmsReviewService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: WmsReviewController
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @Author Jane
 * @Date Tue Jan 30 10:19:45 CST 2018
 */
@RestController
@RequestMapping("/wmsreview")
@Api(value = "/wmsreview", description = "出库复核")
public class  WmsReviewController extends BaseController<WmsReviewService,WmsReview> {

    @GetMapping(value = "/getXmx/{xbh}")
    @ApiOperation(value = "出库复核",response = JSONObject.class, notes = "出库复核扫码页面")
    public @ResponseBody
    JSONObject getXmx(@PathVariable String xbh, HttpSession session) {
        try {
            JSONObject json = baseService.getXmx(xbh,session);
            return SetStatus.setFunctionType(ReturnType.messageType_top.getType(),ReturnType.success.toString(),"出库复核扫码成功","", json.toString());
        } catch (ServiceException e){
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(),ReturnType.error.toString(),"出库复核扫码失败",e.getMessage(),"");
        } catch (Exception e){
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(),ReturnType.error.toString(),"出库复核扫码失败",e.getMessage(),"");
        }
    }

    @GetMapping(value = "/getEdit/{xbh}")
    @ApiOperation(value = "出库复核",response = JSONObject.class, notes = "出库复核扫码页面")
    public @ResponseBody
    JSONObject getEdit(@PathVariable String xbh, HttpSession session) {
        try {
            JSONObject json = baseService.getEdit(xbh,session);
            return SetStatus.setFunctionType(ReturnType.messageType_top.getType(),ReturnType.success.toString(),"出库复核扫码成功","", json.toString());
        } catch (ServiceException e){
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(),ReturnType.error.toString(),"出库复核扫码失败",e.getMessage(),"");
        } catch (Exception e){
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(),ReturnType.error.toString(),"出库复核扫码失败",e.getMessage(),"");
        }
    }

    @PostMapping(value = "/add")
    @ApiOperation(value = "出库复核",response = JSONObject.class, notes = "新增出库复核")
    public @ResponseBody JSONObject add(@RequestBody List<WmsReview> wmsReview, HttpSession session) {
        try {
            baseService.baseInsertSelective(wmsReview,session);
            return SetStatus.setFunctionType(ReturnType.messageType_top.getType(),ReturnType.success.toString(),"新增出库复核成功","", "");
        }catch (SucessException e){
            return SetStatus.setFunctionType(ReturnType.messageType_top.getType(),ReturnType.success.toString(),"新增出库复核功","", "");
        } catch (ServiceException e){
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(),ReturnType.error.toString(),"新增出库复核失败",e.getMessage(),"");
        } catch (Exception e){
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(),ReturnType.error.toString(),"新增出库复核失败",e.getMessage(),"");
        }
    }

    @PostMapping(value = "/edit")
    @ApiOperation(value = "出库复核",response = JSONObject.class, notes = "修改出库复核")
    public @ResponseBody JSONObject edit(@RequestBody List<WmsReview> wmsReview, HttpSession session) {
        try {
            baseService.baseUpdateByPrimaryKeySelective(wmsReview,session);
            return SetStatus.setFunctionType(ReturnType.messageType_top.getType(),ReturnType.success.toString(),"修改出库复核成功","", JSONObject.fromObject(wmsReview).toString());
        }catch (SucessException e){
            return SetStatus.setFunctionType(ReturnType.messageType_top.getType(),ReturnType.success.toString(),"修改出库复核功","", JSONObject.fromObject(wmsReview).toString());
        } catch (ServiceException e){
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(),ReturnType.error.toString(),"修改出库复核失败",e.getMessage(),"");
        } catch (Exception e){
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(),ReturnType.error.toString(),"修改出库复核失败",e.getMessage(),"");
        }
    }

    @PostMapping(value = "/list")
    @ApiOperation(value = "出库复核",response = JSONObject.class, notes = "列表页面查询")
    public @ResponseBody
    JSONObject list(@RequestBody WmsReview entity,HttpSession session) {
        JSONObject json = getJSONObject();
        try {
            UserInfo userinfo= (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
            entity.setCkid(String.valueOf(userinfo.getUser().getOrgId().get(0)));
            Page<Map<String,Object>> list = baseService.baseSelectPage(entity);
            json = SetStatus.setSearchType(JSONArray.fromObject(list).toString(),(int) list.getTotal());
        }catch (Exception e){
            e.printStackTrace();
        }
        return json;
    }

    @PostMapping(value = "/print")
    @ApiOperation(value = "出库复核",response = JSONObject.class, notes = "打印快递面单 map包含：实际重量 width 箱编号 xbh")
    public @ResponseBody
    JSONObject print(@RequestBody Map<String,Object> map,HttpSession session) {
        try {
            return SetStatus.setFunctionType(ReturnType.messageType_top.getType(),ReturnType.success.toString(),"打印快递面单成功","",baseService.print(map,session));
        }catch (ServiceException e){
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(),ReturnType.error.toString(),"打印快递面单失败",e.getMessage(),"");
        }catch (Exception e){
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(),ReturnType.error.toString(),"打印快递面单失败",e.getMessage(),"");
        }
    }
}
